﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.ServiceModel;
using System.Web;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.InterceptionExtension;
using WmsService.BusinessLogic;
using WmsService.Security;

namespace WmsService.AOP
{
    public class PayApplyBillDtlHistoryCallHandler : ICallHandler
    {
        public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
        {
            //开始获取实体
            string payApplyBillID = null;
            string invoiceNo = null;
            PayApplyBillDtl payApplyBillDtl = null;

            if(input.Arguments.Count != 2)
            {
                throw new AopException("[PayApplyBillDtlHistoryCallHandler] 特性绑定方法错误：" +input.MethodBase.Name);
            }

            payApplyBillID = (String)input.Arguments[0]; 
            if(input.Arguments[1] is InvoiceBill)
            {
                invoiceNo = ((InvoiceBill)input.Arguments[1]).InvoiceNo;
            }
            else
            {
                invoiceNo = (String)input.Arguments[1];
            }
            var logic = (IInvoiceAndPayApplyBillLogic)input.Target;
            payApplyBillDtl = logic.GetPayApplyBillDtl(payApplyBillID, invoiceNo);
            
            //执行方法，如果没有异常，就执行下面的保存历史记录方法
            var methodReturn = getNext()(input, getNext);

            var payApplyBillDtlHistory = PayApplyBillDtlHistory.
                CreatePayApplyBillDtlHistory(0, payApplyBillDtl.PayApplyBillDtlID, payApplyBillDtl.PayApplyBillID, payApplyBillDtl.Idx);

            var propertyInfos = payApplyBillDtl.GetType().GetProperties();

            PropertyInfo pi;
            foreach (var propertyInfo in propertyInfos)
            {
                pi = typeof (PayApplyBillDtlHistory).GetProperty(propertyInfo.Name);
                if(pi != null && pi.Name != "EntityKey")
                {
                    try
                    {
                        pi.SetValue(payApplyBillDtlHistory, propertyInfo.GetValue(payApplyBillDtl, null), null);
                    }
                    catch (Exception)
                    {}
                }
            }

            payApplyBillDtlHistory.OperationTime = DateTime.Now;
            payApplyBillDtlHistory.ExtVarChar3 = "删除该记录";
            var logicBase = (LogicBase)input.Target;
            payApplyBillDtlHistory.ExtVarChar4 = logicBase.ContextUserID;

            var ctx = Factory.ContextFactory.Create();
            ctx.AddToPayApplyBillDtlHistory(payApplyBillDtlHistory);
            ctx.SaveChanges();

            return methodReturn;
        }
        public int Order { get; set; }
    }

    public class PayApplyBillDtlHistoryCallHandlerAttribute : HandlerAttribute
    {
        public override ICallHandler CreateHandler(IUnityContainer container)
        {
            return new PayApplyBillDtlHistoryCallHandler { Order = this.Order };
        }
    }
}